const dao = require("../Dao/Dao.js")
const tool = require("../tool")
module.exports = {
    add(req, resp) {
        let param = []
        for (let i in req.body) {
            param.push(req.body[i])
        }
        let sql = `CALL addWork(?,?,?,?,?,?,?,?,?,?,"")`
        dao.do(sql, param, req.query['timer'], function (err, data) {
            resp.send(data)
            err ? console.log(err) : null
        })
    },
    save(req, resp) {
        let param = []
        for (let i in req.body) {
            param.push(req.body[i])
        }
        let sql = `UPDATE \`work\` SET \`name\` = ? , \`start\` = ? , \`end\` = ? ,\`fileid\` = ? , \`other\` = ? ,  \`order\` = ? , \`dos\` = ?,\`nameList\`=? WHERE \`id\` = ?; `
        dao.do(sql, param, req.query['timer'], function (err, data) {
            resp.send(data)
            err ? console.log(err) : null
        })
    },
    async getPaper(req, resp) {
        let { id:wId } = req.query
        let data = await database.work.findOne({ where: { wId } })
        if (data.dataValues.fromid == req.query.uId) {
            data.dataValues.AC = "11111"//查看 编辑 删除 修改cansee 备用
        } else if ((data.dataValues.cansee || "").split(",").includes(req.query.uId)) {
            data.dataValues.AC = "10001"
        }
        resp.send(data.dataValues.AC ? data.dataValues : { code: -2 })
    },
    setCanSee(req, resp) {
        let param = [req.query['cansee'], req.query['id']]
        let sql = `UPDATE \`work\` SET \`cansee\` = ? WHERE \`id\` = ?; `
        dao.do(sql, param, req.query['timer'], function (err, data) {
            resp.send(data)
            err ? console.log(err) : null
        })
    },
    async getMyWork(req, resp) {
        resp.send(await database.work.findAll({ where: { fromid: tool.decodeToken(req.query.uToken) } }))
    },
    async delMyWork(req, resp) {
        resp.send(await database.work.destroy({ where: { id: req.query.workid, fromid: tool.decodeToken(req.query.uToken) } }))
    },
    getMyDoWork(req, resp) {
        let id = tool.decodeToken(req.query.id)
        let param = [id]
        let sql = `SELECT * FROM \`work\` `
        dao.do(sql, param, req.query['timer'], function (err, data) {
            data.data = data.data.filter((line) => {
                if (line.cansee) {
                    return line.cansee.split(",").indexOf(id) !== -1
                }
            })
            resp.send(data)
            err ? console.log(err) : null
        })
    }
}